"""王博文  54616977613
    纸条说  104588375601
    王稳健       96560168975
    严伯钧     105595202170
    毒角SHOW   7869942119
    一条小团团OvO    72673737181

"""
import re
import time

from bs4 import BeautifulSoup
import requests
from lxml import etree
from spider_app.douyin.handle_db import handle_get_task,save_user_info


# 这里文件可以是个变量
def get_decode():
    font_dict = {}
    glyphID = {
        'x': '',
        'num_': 1,
        'num_1': 0,
        'num_2': 3,
        'num_3': 2,
        'num_4': 4,
        'num_5': 5,
        'num_6': 6,
        'num_7': 9,
        'num_8': 7,
        'num_9': 8
    }
    with open('douyin22.xml', 'r') as f:
        str = f.read()

        result_list = re.findall('code="0(.*?)" name="(.*)"', str)
        for list in result_list:
            key = ' &#' + list[0] + '; '
            font_dict[key] = glyphID[list[1]]
        return font_dict


# 替换关键字
def replace_number(list, dict1):
    result = []
    for i in list:

        if i in dict1:
            number = str(dict1[i])
            result.append(number)
        else:
            result.append(i)
    number = ''.join(result)
    return number


def handle_douyin_web_share(task):
    share_url = 'https://www.iesdouyin.com/share/user/{}'.format(task['share_id'])
    share_header = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) '}
    share_res = requests.get(share_url, headers=share_header)
    share_res.encoding = share_res.apparent_encoding
    text = share_res.text

    html = etree.HTML(text)

    # 数据存储表
    user_info = {}
    if not html.xpath('//p[@class="nickname"]/text()'):
        print('灭有')
        return None
    user_info['nickname'] = html.xpath('//p[@class="nickname"]/text()')[0]
    font_dict = get_decode()

    # id提取
    face_number_list = re.findall(r'<i class="icon iconfont ">(.*?)</i>', text)
    id = replace_number(face_number_list, font_dict)
    head = html.xpath('//p[@class="shortid"]/text()')[0]
    id = head + id
    user_info['id'] = id

    # 职业
    job_div = html.xpath('//div[@class="verify-info"]')
    if len(job_div) != 0:
        job = job_div[0].xpath('.//span/text()')[0].strip()
        user_info['job'] = job

    # 个性签名
    signature = html.xpath('//p[@class="signature"]/text()')
    if len(signature) != 0:
        user_info['signature'] = signature[0]

    # 关注数
    span_focus = re.findall(r'<span class="focus block">(.*?)</span>', text)[0]
    number_list = re.findall(r'<i class="icon iconfont follow-num">(.*?)</i>', span_focus)
    number_focus = replace_number(number_list, font_dict)
    user_info['number_focus'] = number_focus

    # 粉丝数

    follower = re.findall(r'<span class="follower block">(.*?)</span>', text)[0]
    number_followers = re.findall(r'<i class="icon iconfont follow-num">(.*?)</i>', follower)
    number_follower = replace_number(number_followers, font_dict)


    follower_block = html.xpath('//span[@class="follower block"]/span//text()')
    if 'w ' in follower_block:
        number = int(number_follower)/10
        number0 = str(number) + 'w'
    else:
        number0=number_follower
    user_info['fans_number'] = number0

    # 点赞数
    span = re.findall(r'<span class="liked-num block">(.*?)</span>', text)[0]
    number_list = re.findall(r'<i class="icon iconfont follow-num">(.*?)</i>', span)
    number2 = replace_number(number_list, font_dict)
    like_list = html.xpath('//span[@class="liked-num block"]//text()')
    if '.' in like_list:
        number2 = int(number2) / 10
    if 'w ' in like_list:
        number2 = str(number2) + 'w'
    user_info['like_number'] = number2
    print(user_info)
    save_user_info(user_info)

while True:
     task = handle_get_task()
     # task={'share_id':'86314100721'}
     handle_douyin_web_share(task)
     time.sleep(1)
